//
//  GHGoodDetailViewController.m
//  GodHorses
//
//  Created by Mac on 2017/11/18.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "GHGoodDetailViewController.h"
#import "GHGoodDetailView.h"
#import "GHGoodDetailModel.h"
#import "GHGoodBottomView.h"
#import "GHCommentTableViewCell.h"
#import "GHGoodMenuView.h"
#import "GHShopCardViewController.h"
@interface GHGoodDetailViewController ()<GHGoodDetailViewDelegate>
@property(nonatomic, strong) GHGoodDetailView                       *header;
@property(nonatomic, strong) GHGoodDetailModel                      *goodDetailModel;
@property(nonatomic, strong) UIImageView                            *bannerImageView;
@property(nonatomic, strong) GHGoodMenuView                         *menuView;
@property(nonatomic, strong) GHGoodBottomView                       *bootomView;
@property(nonatomic, assign) NSString                               *chosedGoodNumString;//选择规格的数量
@end

@implementation GHGoodDetailViewController
- (void)setGoodDetailModel:(GHGoodDetailModel *)goodDetailModel{
    _goodDetailModel = goodDetailModel;
    self.menuView.goodModel = goodDetailModel.goodModel;
}

#pragma mark --vc 生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"商品详情";
    [self configSubView];
    // Do any additional setup after loading the view.
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [GHUtils netWorkMonitorinCanUseWithHandle:^{
        [self sendGoodDetailRequest];
    }];
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
}
#pragma mark --subView
- (void)configSubView{
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.height = self.tableView.height - 50;
    //baner
    self.bannerImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, GHIconHeaderHeight)];
    self.bannerImageView.image = [UIImage imageNamed:@"img_banner_3"];
    [self.view insertSubview:self.bannerImageView atIndex:0];
    
    self.header = [[GHGoodDetailView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 200)];
    self.header.delegate = self;
    self.tableView.tableHeaderView = self.header;
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self sendGoodDetailRequest];
    }];
    [self.tableView.mj_header beginRefreshing];
    
    //底部
    __weak typeof(self) weakSelf =  self;
    self.bootomView = [[GHGoodBottomView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 50) handle:^(NSString *title) {
        [weakSelf onSelectedBootomButtonWithTitle:title];
    }];
    self.bootomView.backgroundColor = [UIColor whiteColor];
    [self.bootomView st_showTopShadow];
    self.bootomView.bottom = UIScreenHeight -64;
    [self.view addSubview:self.bootomView];
    
    //menu
    self.menuView = [[GHGoodMenuView alloc] initWithFrame:SCREEN_FRAME];
    self.menuView.backgroundColor = [UIColor clearColor];
}
#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.goodDetailModel.comments.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    GHCommentTableViewCell *cell = (GHCommentTableViewCell*)[self tableView:self.tableView cellForRowAtIndexPath:indexPath];
    cell.model = self.goodDetailModel.comments[indexPath.row];
    return cell.model.height;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 24;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView * header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 24)];
    header.backgroundColor = [UIColor whiteColor];
    return header;
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * reuseIdentifier =  @"cell";
    GHCommentTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[GHCommentTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        
    }
    cell.model = self.goodDetailModel.comments[indexPath.row];
    return cell;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView == self.tableView) {
        //滑动动画
        CGFloat y = self.tableView.contentOffset.y;
        //NSLog(@"滑动了%f",y);
        if (y  > 0) {
            self.bannerImageView.top =  - 0.35 * y;
        }else{
            self.bannerImageView.top = - y;
        }
        
    }
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}
#pragma mark --Private Method
- (GHGoodDetailModel*)detailModelFromDic:(NSDictionary*)responseObject{
    GHGoodDetailModel * model = [GHGoodDetailModel new];
    NSDictionary * dataDic = responseObject[@"data"];
    if ([dataDic.allKeys containsObject:@"goods_info"]) {
        GHGoodModel * good = [GHGoodModel mj_objectWithKeyValues:dataDic[@"goods_info"]];
        model.goodModel = good;
    }
    if ([dataDic.allKeys containsObject:@"comment_info"]) {
        NSArray * array = dataDic[@"comment_info"];
        NSMutableArray * finshArray = [NSMutableArray new];
        for (NSDictionary *commentDic in array) {
            GHCommentModel * comment = [GHCommentModel mj_objectWithKeyValues:commentDic];
            [finshArray addObject:comment];
        }
        model.comments = finshArray;
    }
    return model;
}
#pragma mark --Action Method
#pragma mark --GHGoodDetailViewDelegate
- (void)goodDetailView:(GHGoodDetailView *)goodDetailView onSelectedProductNumButton:(UIButton *)productNumButton{
    //
    NSLog(@"点击了数量按钮");
    __weak typeof(self) weakSelf =  self;
    [self.menuView showFromView:self.view.window handle:^(NSString *num) {
        weakSelf.chosedGoodNumString = num;
    }];
}
- (void)onSelectedBootomButtonWithTitle:(NSString*)title{
    if (!self.goodDetailModel) {
        //如果进入页面加载失败，没有goodtetailModel 则先请求数据
        [self sendGoodDetailRequest];
        return;
    }
    if ([title isEqualToString:@"收藏"]) {
        [self sendCollectionRequest];
    }
    if ([title isEqualToString:@"购物车"]) {
        [self.navigationController pushViewController:[GHShopCardViewController new] animated:YES];
    }
    if ([title isEqualToString:@"加入购物车"]) {
        
        //选择规格
        __weak typeof(self) weakSelf =  self;
        [self.menuView showFromView:self.view.window handle:^(NSString *num) {
            weakSelf.chosedGoodNumString = num;
            [self sendAddCarRequest];
        }];
        
    }
    if ([title isEqualToString:@"立刻购买"]) {
        
    }
}
#pragma mark --NetWork Method
- (void)sendGoodDetailRequest{
    [SVProgressHUD showWithStatus:@"正在加载"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    if ([GHUserManger defult].userID.length) {
        [dic setObject:[GHUserManger defult].userID forKey:@"uid"];
    }
    if ([GHUserManger defult].token.length) {
        [dic setObject:[GHUserManger defult].userID forKey:@"goods_id"];
    }
    if (self.goodID.length) {
        [dic setObject:self.goodID forKey:@"goods_id"];
    }
    [[STNetWrokManger defaultClient] requestWithPath:@"http://test.smqyp.com/Api/goods/get_goods_detail"
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 self.goodDetailModel = [self detailModelFromDic:responseObject];
                                                 [self.tableView.mj_header endRefreshing];
                                                 self.header.goodModel = self.goodDetailModel.goodModel;
                                                 [self.bannerImageView sd_setImageWithURL:[NSURL URLWithString:self.goodDetailModel.goodModel.g_img] placeholderImage:GH_placeHoderImage];
                                                 [self.tableView reloadData];
                                                 [SVProgressHUD dismiss];
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [self.tableView.mj_header endRefreshing];
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                             }];
    
    
    
}
- (void)sendCollectionRequest{
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    if ([GHUserManger defult].userID.length) {
        [dic setObject:[GHUserManger defult].userID forKey:@"uid"];
    }
    if ([GHUserManger defult].token.length) {
        [dic setObject:[GHUserManger defult].token forKey:@"token"];
    }
    if (self.goodDetailModel.goodModel.goodId.length) {
        [dic setObject:self.goodDetailModel.goodModel.goodId forKey:@"g_id"];
    }
    [[STNetWrokManger defaultClient] requestWithPath:@"http://test.smqyp.com/Api/order/add_collection"
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [SVProgressHUD showSuccessWithStatus:@"收藏成功"];
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                             }];
}
- (void)sendAddCarRequest{
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    if ([GHUserManger defult].userID.length) {
        [dic setObject:[GHUserManger defult].userID forKey:@"uid"];
    }
    if ([GHUserManger defult].token.length) {
        [dic setObject:[GHUserManger defult].token forKey:@"token"];
    }
    if (self.goodDetailModel.goodModel.goodId.length) {
        [dic setObject:self.goodDetailModel.goodModel.goodId forKey:@"goods_id"];
    }
    if (self.chosedGoodNumString.length) {
        [dic setObject:self.chosedGoodNumString forKey:@"goods_number"];
    }
    [[STNetWrokManger defaultClient] requestWithPath:@"http://test.smqyp.com/Api/goods/add_shopping_cart"
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [SVProgressHUD showSuccessWithStatus:@"添加到购物车成功"];
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                             }];
}
@end

